package com.wcy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.common.CommonResult;
import com.wcy.entity.Role;
import com.wcy.entity.UserRole;
import com.wcy.service.IRoleService;
import com.wcy.service.IUserRoleService;
import com.gaofei.domain.FileResult;
import com.gaofei.service.FileService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-04-30
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @Autowired
    IUserRoleService userRoleService;

    @Autowired
    FileService fileService;
    /**
     * 列表
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    public PageInfo<Role> list(String name, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "2") int pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<Role> list = roleService.getRoleList(name);
        PageInfo<Role> info = new PageInfo<>(list);
        return info;
    }
    /**
     * 根据用户id,来查询角色
     * @param uid
     * @return
     */
    @RequestMapping("findRolesByUid")
    public CommonResult findRolesByUid(Integer uid){
        QueryWrapper<UserRole> userRoleWrapper = new QueryWrapper();
        userRoleWrapper.eq("uid", uid);
        //1.根据用户id查询用户角色表
        //这个数据就是用户角色的数据的封装-->角色复选框回显
        List<UserRole> list = userRoleService.list(userRoleWrapper);
        ArrayList<Role> roles = new ArrayList<>();
        //根据角色id查询对应的角色
        list.forEach(userRole -> {
            Role byId = roleService.getById(userRole.getRid());
            //把查询出来的角色存入集合
            roles.add(byId);
        });
        return CommonResult.success(roles);
    }


    @RequestMapping("upload")
    public  Object upload(MultipartFile[] file){

        List<FileResult> upload = fileService.upload(file);
        System.out.println(upload);
        return  upload;
    }

    @RequestMapping("toadd")
    public int toadd(@RequestBody Role role){
        if(role.getId()==null){
            int i = roleService.addRole(role);
            return i;
        }else if(role.getId()!=0 && role.getId()!= null){
            int i = roleService.updateRole(role);
            return i;
        }
        return 0;
    }

    @RequestMapping("getRole")
    public Role queryMyfile(int id){
        Role role = roleService.getRoleById(id);
        System.out.println(role);
        return role;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("del")
    public int del(int id){
        int i = roleService.delRole(id);
        return i;
    }


}

